package com.jhgsys.internal.base.service.impl;

import com.jhgsys.internal.base.entity.BaseProject;
import com.jhgsys.internal.base.entity.BaseProjectUser;
import com.jhgsys.internal.base.mapper.BaseProjectUserMapper;
import com.jhgsys.internal.base.service.IBaseProjectUserService;
import com.jhgsys.internal.common.entity.FebsConstant;
import com.jhgsys.internal.common.entity.QueryRequest;
import com.jhgsys.internal.common.utils.FebsUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.annotation.Propagation;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.toolkit.StringPool;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 项目用户 Service实现
 *
 * @author admin
 * @date 2024-07-29 15:56:15
 */
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class BaseProjectUserServiceImpl extends ServiceImpl<BaseProjectUserMapper, BaseProjectUser> implements IBaseProjectUserService {

    /**
     * 查询（分页）
     * @author admin
     * @date 2024-07-29 15:56:15
     * @param request QueryRequest
     * @param baseProjectUser baseProjectUser
     * @return IPage<BaseProjectUser>
    */
    @Override
    public IPage<BaseProjectUser> findBaseProjectUsers(QueryRequest request, BaseProjectUser baseProjectUser) {
        LambdaQueryWrapper<BaseProjectUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BaseProjectUser::getDeletedFlg, FebsConstant.VALID);
        // TODO 设置查询条件

        Page<BaseProjectUser> page = new Page<>(request.getPageNum(), request.getPageSize());
        FebsUtil.pageWrapper(page, request);
        return this.page(page, queryWrapper);
    }

    /**
     * 查询（所有）
     * @author admin
     * @date 2024-07-29 15:56:15
     * @param baseProjectUser baseProjectUser
     * @return List<BaseProjectUser>
     */
    @Override
    public List<BaseProjectUser> findBaseProjectUsers(BaseProjectUser baseProjectUser) {
	    LambdaQueryWrapper<BaseProjectUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BaseProjectUser::getDeletedFlg, FebsConstant.VALID);
        if(StringUtils.isNotBlank(baseProjectUser.getProjectId())){
            queryWrapper.eq(BaseProjectUser::getProjectId, baseProjectUser.getProjectId());
        }
		return this.baseMapper.selectList(queryWrapper);
    }

    /**
    * 根据ID查询
    * @author admin
    * @date 2024-07-29 15:56:15
    * @param id
    * @return BaseProjectUser
    */
    @Override
    public BaseProjectUser findBaseProjectUserById(Integer id){
        return this.baseMapper.selectById(id);
    }

    /**
     * 新增
     * @author admin
     * @date 2024-07-29 15:56:15
     * @param baseProjectUser baseProjectUser
     */
    @Override
    @Transactional
    public void createBaseProjectUser(BaseProjectUser baseProjectUser) {
        baseProjectUser.setCreatedTime(new Date());
        this.save(baseProjectUser);
    }

    /**
     * 修改
     * @author admin
     * @date 2024-07-29 15:56:15
     * @param baseProjectUser baseProjectUser
     */
    @Override
    @Transactional
    public void updateBaseProjectUser(BaseProjectUser baseProjectUser) {
        baseProjectUser.setUpdatedTime(new Date());
        this.saveOrUpdate(baseProjectUser);
    }

    /**
     * 逻辑删除
     * @author admin
     * @date 2024-07-29 15:56:15
     * @param baseProjectUser baseProjectUser
     * @param baseProjectUserIds baseProjectUserIds
     */
    @Override
    @Transactional
    public void deleteBaseProjectUser(BaseProjectUser baseProjectUser,String baseProjectUserIds) {
        List<String> list = Arrays.asList(baseProjectUserIds.split(StringPool.COMMA));
        this.baseMapper.update(baseProjectUser,new LambdaQueryWrapper<BaseProjectUser>().in(BaseProjectUser::getProjectUserId,list));
	}

    /**
    * 物理删除
    * @author admin
    * @date 2024-07-29 15:56:15
    * @param baseProjectUserIds baseProjectUserIds
    */
    @Override
    @Transactional
    public void physicallyDeleteBaseProjectUser(String baseProjectUserIds) {
        List<String> list = Arrays.asList(baseProjectUserIds.split(StringPool.COMMA));
        LambdaQueryWrapper<BaseProjectUser> wapper = new LambdaQueryWrapper<>();
        wapper.in(BaseProjectUser::getProjectUserId,list);
        this.remove(wapper);
    }

    /**
    * 根据项目id删除
    * @author admin
    * @date 2024-07-29 15:56:15
    * @param projectIds
    */
    @Override
    @Transactional
    public void deleteByProjectIds(String projectIds) {
        List<String> list = Arrays.asList(projectIds.split(StringPool.COMMA));
        LambdaQueryWrapper<BaseProjectUser> wapper = new LambdaQueryWrapper<>();
        wapper.in(BaseProjectUser::getProjectId,list);
        this.remove(wapper);
    }
}
